package com.briup.test;

/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/9/29 17:19
 * @package com.briup.test
 * @copyright 甜圆
 */
public class DaemonExample {
    public static void main(String[] args) {
        // 创建守护线程（为主线程服务）
        Thread daemonThread = new Thread(() -> {
            while (true) {
                System.out.println("守护线程在运行...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        daemonThread.setDaemon(true); // 设置为守护线程
        daemonThread.start();

        System.out.println("主线程工作3秒后结束");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程结束，守护线程也会自动终止");
        // 因为daemonThread是守护线程，主线程结束后JVM退出
    }
}